module RadiaStore
  module StorageSystem
    
  	# CLASS UploadSystem: Handles the Upload requests to any Replication System
  	class UploadSystem
  	  def initialize
  	    @storage_path = "#{MAIN_REPOSITORY_PATH}"
  	    @main_repository = ReplicaSystem.find_main_repository
  	  end
  	
  	  def store_file(filename, file_extension, data)
  	    begin
    		  path = @storage_path + filename + file_extension
    		  File.open(path, "wb") { |file| file.write(data) }
    		  # returns path to store on Replica object, and the identification of ReplicationSystem
    		  [path, @main_repository.id]
  	    rescue
  		    raise FileNotStoredException.new
  	    end
  	  end
  	end
  	
  	class DownloadSystem 
  	  include ActionController::Streaming
  
  	  def initialize
  	  end
  	  
  	  # audio_asset must be an AudioAsset Object
  	  # TODO: test if the file not exists and try to find it in other replicas
  	  def get_essence_file(audio_asset)
  	    audio_asset.find_main_essence.replica_asset_uri
  	  end
  	  
  	  # audio_asset must be an AudioAsset Object
  	  # TODO: test if the file not exists and try to find it in other replicas
  	  def get_metadata_file(audio_asset)
  	    audio_asset.find_main_metadata.replica_asset_uri
  	  end
  	end
  	
  	#################################
  	# Exceptions
  	#################################
  	class FileNotStoredException < Exception
    end
 
  end
end